from utils.prettyrepr import prettify_class
from utils.relations import One


@prettify_class
class Entity(object):
    __slots__ = ("id", "_tile", "_active")
    __info_attrs__ = ("id", "tile", "active")

    def __init__(self, id, tile=None):
        self.id = id
        self._tile = None
        self._active = False
        if tile is not None:
            self.tile = tile

    @One(complement="entities")
    def tile(self):
        return self._tile

    @tile.setter
    def tile(self, tile):
        self._tile = tile

    @property
    def maze(self):
        tile = self._tile
        return None if tile is None else tile.maze

    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, active):
        active = bool(active)
        if active == self._active:
            return
        tile = self._tile
        if tile is not None:
            maze = tile.maze
            if active and not self._active:
                tile.active_entities.add(self)
                maze.active_entities.add(self)
            elif not active and self._active:
                tile.active_entities.remove(self)
                maze.active_entities.remove(self)
        self._active = active

    @property
    def maze(self):
        tile = self._tile
        return None if tile is None else tile.maze

    @property
    def time(self):
        tile = self._tile
        return None if tile is None else tile.maze.time

    # --------------------------------------------------------------------------
    # Game management methods
    def init(self):
        """Called by the maze on init()."""
        pass

    def step(self, dt):
        """Step forward in time."""
        pass

    def post_step(self):
        """Called after each step() to check for things that should be happening simultaneously to
        all entities (e.g. all entities should move first, and only then check for collisions)."""
        pass
